package com.bridgetree.jubaocrm.ui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bridgetree.jubaocrm.R;
import com.bridgetree.jubaocrm.util.Transaction;

/**
 * 事务适配器
 */
public class PageTransactionAdapter extends BaseAdapter {
	private List<Transaction> Date = null;
	private Context context;
	private LayoutInflater mInflater;
	public HashMap<Integer, Bitmap> cachedBitmap;

	private class GridHolder {
		ImageView xm_12_flag_iv;
		TextView tv;
	}

	@SuppressLint("UseSparseArrays")
	public PageTransactionAdapter(Context c,List<Transaction> proInfoListData) {
		super();
		this.context = c;
		this.Date=proInfoListData;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return Date.size();
	}

	@Override
	public Object getItem(int index) {
		return Date.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(final int index, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.xm_12_my_item, null);
			holder = new GridHolder();
			holder.xm_12_flag_iv = (ImageView) convertView.findViewById(R.id.xm_12_flag_iv);
			holder.tv = (TextView) convertView.findViewById(R.id.xm_12_item_tv);
			convertView.setTag(holder);
		} else {
			holder = (GridHolder) convertView.getTag();
		}

		final String xm_txt=Date.get(index).getReminder_time();
		
		String sreturn = "";
		if (xm_txt != null && !xm_txt.equals("") ) { // 处理空值情况
			String s_nd = xm_txt.substring(0, 4); // 年份
			String s_yf = xm_txt.substring(5, 7); // 月份
			String s_rq = xm_txt.substring(8, 10); // 日期
			String s_hh = xm_txt.substring(11, 13); // 小时
			String s_fen = xm_txt.substring(14, 16); // 分钟
			// 处理间隔符
			sreturn = s_nd + "年" +s_yf + "月"
			+s_rq + "日  "+s_hh + "点"+s_fen + "分钟";
		}
		holder.tv.setText(sreturn);
		
		holder.xm_12_flag_iv.setImageResource(R.drawable.edit);
		holder.xm_12_flag_iv.setVisibility(View.VISIBLE);

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,TransactionDetailsActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("Transaction", Date.get(index));
				intent.putExtras(bundle);
				((Activity) context).startActivityForResult(intent, 10005);
			}
		});
		return convertView;
	}

	/**
	 * 判断日期格式是否正确
	 */
	@SuppressLint("SimpleDateFormat")
	public boolean IsDateFormat(String dataStr) {
		boolean state = false;
		try {
			java.text.SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd"); 
			dFormat.setLenient(false); 
			java.util.Date d = dFormat.parse(dataStr);
			state = true;
		} catch (ParseException e) {
			e.printStackTrace();
			state = false;
		} 
		return state;
	}

	@SuppressLint("SimpleDateFormat")
	public String getStringDateMonth(String sdate) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		String dateString = formatter.format(currentTime);
		String s_nd = dateString.substring(0, 4); // 年份
		String s_yf = dateString.substring(5, 7); // 月份
		String s_rq = dateString.substring(8, 10); // 日期
//		2014-05-17 09:06
		String s_hh = dateString.substring(11, 13); // 小时
		String s_fen = dateString.substring(14, 16); // 分钟
		String sreturn = "";
		if (sdate != null && !sdate.equals("") ) { // 处理空值情况
			// 处理间隔符
			sreturn = s_nd + "年" +s_yf + "月"
			+s_rq + "日  "+s_hh + "点"+s_fen + "分钟";
		}
		return sreturn;
	}
}

